//utf-8
//点击新增按钮 事件跳转至 新增页面
function toAddData(){
	var dialogStruct={
		'display':contextRootPath+'/tCMmCreditDetail/prepareExecute/toAdd',
		'width':800,
		'height':500,
		'title':'新增',
		'isIframe':'false',
		'buttons':[
         {'text':'保存','action':doAddFrom,'isClose':true},
		 {'text':'关闭','isClose':true}
		]
	};
	
	var dialogAdd =jyDialog(dialogStruct).open();
}
//新增页面的保存操作
function doAddFrom(){
	//序列化 新增页面的form表单数据
	var params=$("#addNewsFormData").serialize();
	var url=contextRootPath+'/tCMmCreditDetail/insertTCMmCreditDetail';
	//通过ajax保存
	jyAjax(
		url,
		params,
		function(msg){
			//新增成功后，
			alert(msg.msg);
			var v_status = msg.status;
        	if(v_status.indexOf('ok') >-1){
        		//新增成功后 刷新页面 或 只查询 id为msg.data['id'] 的  数据
        		queryData();
        	}
  	});
}
//修改 事件
var dialogUpdate;
function toUpdateData(){
	var v_ids = iframe.iframeObj["table"].getSelectedIds().join(",");
	var v_loanStatus = iframe.iframeObj["table"].getSelectedObjs("creditorStatus").join(",");
	//如果没有选中 数据则
	if(v_ids == ""){
		alert("请选择待修改的数据！");
		return;
	}
	//如果选择多个 择提示
	if(v_ids.indexOf(",") > -1){
		alert("请选择一条数据！");
		return;
	}
	
//	if(v_loanStatus!='01'){
//		 alert("只有债权状态为“在途”可以修改。");
//	     return;
//	}

	var dialogStruct={
			'display':contextRootPath+'/tCMmCreditDetail/prepareExecute/toUpdate?id='+v_ids,
			'width':1200,
			'height':600,
			'title':'修改',
			'isIframe':'false',
			'buttons':[
             {'text':'提交','action':doUpdateFrom,'isClose':true},
			 {'text':'关闭','action':removeAttr,'isClose':true}
			]
		};
		
    dialogUpdate = jyDialog(dialogStruct);
    dialogUpdate.open();
}


//导出贷款信息exportForm
function exportForm(){
	var transferId=$("input[name=transferId]").val();
	var creditCode=$("input[name=creditCode]").val();
	var packageNum=$("input[name=packageNum]").val();
	var beginDeliverDate=$("input[name=beginDeliverDate]").val();
	var endDeliverDate=$("input[name=endDeliverDate]").val();
	var cooperorgCode=$("#cooperorgCode").val();
	var contractCode=$("#contractCode").val();
	//var agreementCode=$("#agreementCode").val();
	var creditorStatus=$("select[name=creditorStatus]").val();
	var coreState=$("select[name=coreState]").val();
	var loanEachStatus=$("select[name=loanEachStatus]").val();
	var cooperorgPackageName = $("input[name=cooperorgPackageName]").val();
	if(null==cooperorgCode ||""==cooperorgCode){
		alert("请选择合作机构！");
		return;
	}
	var dataUrl = contextRootPath + '/tCMmCreditDetail/exportCreditDetail?transferId=' + transferId+ "&creditCode="+creditCode+"&packageNum="+packageNum+ "&beginDeliverDate="+beginDeliverDate+ "&endDeliverDate="+endDeliverDate+ "&cooperorgCode="+cooperorgCode+ "&contractCode="+contractCode+"&creditorStatus="+creditorStatus+"&coreState="+coreState+"&loanEachStatus="+loanEachStatus +"&cooperorgPackageName=" + cooperorgPackageName;
	//var dataUrl = contextRootPath + '/tCMmLoanDetail/exportLoanDetail?contractNo=' + contractNo+ "&custCode="+custCode+ "&dealCode="+dealCode+ "&creditPackageName="+creditPackageName+ "&cooperorgCode="+cooperorgCode+ "&contractCode="+contractCode"&agreementCode="+agreementCode"&loanStatus="+loanStatus"&abnormalStatus="+abnormalStatus;
	window.location = dataUrl;

}


//补录债权明细的计划赎回日期
var dialog_import = null;
function importCreditForPlanRedeemDate(){
	
	var importUrl = "tCMmCreditDetail/importCreditForPlanRedeemDate";	
	var callBack = 'creditSettleBillBack()';
		
	var dialogStruct={
		'display':contextRootPath+'/common/importExcelFile.jsp?importUrl=' +importUrl + 
		 "&callBack="+callBack,
		'width':800,
		'height':400,
		'title':'导入数据',
		'isIframe':'true',
		'buttons':[{'text':'关闭','isClose':true}]
	};
	dialog_import =jyDialog(dialogStruct);
	dialog_import.open();
}

function creditSettleBillBack(){
	//查询修改后的数据
	queryData();
	return "处理成功";
}

//修改页面 的 保存操作
function doUpdateFrom(dialogUpdate){
	//序列化 新增页面的form表单数据
	var params=$("#updateNewsFormData").serialize();
	var url=contextRootPath+'/tCMmCreditDetail/updateTCMmCreditDetailStatus';
	//通过ajax保存
	jyAjax(
		url,
		params,
		function(msg){
			//保存成功后，执行查询页面查询方法
			alert(msg.msg);
        	var v_status = msg.status;
        	if(v_status.indexOf('ok') >-1){
        		//新增成功后 刷新页面 或 只查询 id为msg.data['id'] 的  数据
        		
//        		var winObj = parent.tabs.getTabWinByTitle("债权查询");
//    			winObj.location.reload();
        		queryData();
        	}
  	});
}

//批量修改 事件
var batch_ids = null;
var dialogBatchUpdate;
function toBatchUpdateData(){
	var v_creditorStatus = iframe.iframeObj["table"].getSelectedObjs("creditorStatus");
	batch_ids = iframe.iframeObj["table"].getSelectedObjs("id");
	
	//如果没有选中 数据则
	if(batch_ids == ""){
		alert("请选择待修改的数据！");
		return;
	}
	
	var creditorStatus = v_creditorStatus[0];
	var checkflag1 = 0;
	var checkflag2 = 0;
	$.each(v_creditorStatus,function(key){
	    	   if(v_creditorStatus[key]!=creditorStatus){
	    		   checkflag1 += 1;
	    	   }
	    	   
	    	   if(v_creditorStatus[key]!='01'){
	    		   checkflag2 += 1;
	    	   }
	    	});
	
	if(checkflag1 > 0){
	    	alert("选中的债权状态不一致,请重新选择");
	    	return;
	 }

	if(checkflag2 > 0){
		 alert("只要债权状态为“在途”可以批量修改。");
	     return;
    }
	
	var dialogStruct={
			'display':contextRootPath+'/tCMmCreditDetail/prepareExecute/toBatchUpdate?creditorStatus='+creditorStatus,
			'width':900,
			'height':300,
			'title':'批量修改',
			'isIframe':'false',
			'buttons':[
             {'text':'提交','action':doBatchUpdateFrom,'isClose':true},
			 {'text':'关闭','isClose':true}
			]
		};
		
	     dialogBatchUpdate = jyDialog(dialogStruct);
	     dialogBatchUpdate.open();
		
}

//修改页面 的 保存操作
function doBatchUpdateFrom(dialogBatchUpdate){
	//序列化 新增页面的form表单数据
	var params;
	var creditorStatus=document.getElementById("updatecreditorStatus").value;
	var url=contextRootPath+'/tCMmCreditDetail/batchupdateTCMmCreditDetail?creditorStatus='+creditorStatus+'&batch_ids='+batch_ids;
	//通过ajax保存
	jyAjax(
		url,
		params,
		function(msg){
			//保存成功后，执行查询页面查询方法
			alert(msg.msg);
        	var v_status = msg.status;
        	if(v_status.indexOf('ok') >-1){
        		//新增成功后 刷新页面 或 只查询 id为msg.data['id'] 的  数据
        		//dialogBatchUpdate.close();
        		queryData();
//        		var winObj = parent.tabs.getTabWinByTitle("债权查询");
//    			winObj.location.reload();
        	}
  	});
}

//删除 事件
function deleteData(){
	var v_ids = iframe.iframeObj["table"].getSelectedIds().join(",");
	//如果没有选中 数据则
	if(v_ids == ""){
		alert("请选择待删除的数据！");
		return;
	}
	if(confirm("您确认要删除选择的数据吗？")){
        $.ajax({
            type:"POST",
            dataType : "json",
            url:contextRootPath+"/tCMmCreditDetail/deleteTCMmCreditDetail?ids="+v_ids,
            success:function(msg){
            	alert(msg.msg);
            	var v_status = msg.status;
            	//删除成功后
            	if(v_status.indexOf('ok') >-1){
            		iframe.iframeObj["table"].removeSelectedRow();
            	}
            	
            	
            }
        });
    }
}
//查看明细
function viewData(vId){
	var dialogStruct={
			'display':contextRootPath+'/tCMmCreditDetail/prepareExecute/toView?id='+vId,
			'width':1000,
			'height':500,
			'title':'查看明细',
			'isIframe':'false',
			'buttons':[
			 {'text':'关闭','isClose':true}
			]
	};
	 
	 var dialogView = jyDialog(dialogStruct);
		dialogView.open();
}
//根据债权编号刷新债权信息
function toBatchUpdateCredit(){
	var v_ids = iframe.iframeObj["table"].getSelectedIds().join(",");
	//如果没有选中 数据则
	if(v_ids == ""){
		alert("请选择待修改的数据！");
		return;
	}
	var v_creditCode;
	var v_creditSource = iframe.iframeObj["table"].getSelectedObjs("creditSource");
	if(v_creditSource==cimsConstant.CREDIT_SOURCE_LOA){
		v_creditCode = iframe.iframeObj["table"].getSelectedObjs("creditCode");
	}else if(v_creditSource==cimsConstant.CREDIT_SOURCE_CRE){
		v_creditCode = iframe.iframeObj["table"].getSelectedObjs("transferId");
	}
	
	
	var params;
	
	var url=contextRootPath+'/tCMmCreditDetail/toBatchUpdateCredit?creditCode='+v_creditCode+'&creditSource='+v_creditSource;
	//通过ajax保存
	jyAjax(
		url,
		params,
		function(msg){
			//保存成功后，执行查询页面查询方法
			alert(msg.msg);
        	var v_status = msg.status;
        	if(v_status.indexOf('ok') >-1){
        		//新增成功后 刷新页面 或 只查询 id为msg.data['id'] 的  数据
        		//dialogBatchUpdate.close();
        		queryData();
//        		var winObj = parent.tabs.getTabWinByTitle("债权查询");
//    			winObj.location.reload();
        	}
  	});
		
}



function removeAttr(){
	$('input').removeAttr("disabled");
	$('input').removeAttr("readonly");
	
}